using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;

namespace The_LarX.Rendering
{
    /// <summary>
    /// Class that manages all SpriteFonts that the engine will automatically load.
    /// </summary>
    public class FontManager
    {
        /// <summary>
        /// A Dictionary containing every SpriteFont the Engine has automatically loaded.
        /// The FontNames(Key) are the SpriteFont's filename.
        /// </summary>
        protected Dictionary<string, SpriteFont> Fonts = new Dictionary<string, SpriteFont>();

        /// <summary>
        /// Dictionary used to Store the Correct string name of a Button, for quick
        /// find.
        /// </summary>
        protected Dictionary<Buttons, string> XboxButtons;

        public FontManager()
        {
            Load();
        }

        private List<string> FontsNames;
        public void Load()
        {
            Fonts.Clear();

            if (FontsNames == null)
            {
#if WINDOWS_PHONE
            FontsNames = LanguageSerializer.LoadListStrings(Path.Combine(Engine.Content.RootDirectory, "Engine\\Fonts\\Fonts.fonts"));
#else
                FileInfo[] infos = new DirectoryInfo(Path.Combine(Engine.Content.RootDirectory, "Engine\\Fonts\\")).GetFiles();
                FontsNames = new List<string>();
                for (int i = 0; i < infos.Length; i++)
                {
                    string final = Path.GetFileNameWithoutExtension(infos[i].Name);
                    FontsNames.Add(final);
                }
#endif
            }

            for (int i = 0; i < FontsNames.Count; i++)
            {
                LoadFont("Engine\\Fonts\\" + FontsNames[i], FontsNames[i]);
            }

            if (XboxButtons == null)
            {
                XboxButtons = new Dictionary<Buttons, string>();

                XboxButtons.Add(Buttons.LeftStick, " ");
                XboxButtons.Add(Buttons.DPadDown | Buttons.DPadLeft |
                    Buttons.DPadRight | Buttons.DPadUp, "!");
                XboxButtons.Add(Buttons.RightStick, '"'.ToString());
                XboxButtons.Add(Buttons.Back, "#");
                XboxButtons.Add(Buttons.BigButton, "$");
                XboxButtons.Add(Buttons.Start, "%");
                XboxButtons.Add(Buttons.X, "&");
                XboxButtons.Add(Buttons.A, "'");
                XboxButtons.Add(Buttons.Y, "(");
                XboxButtons.Add(Buttons.B, ")");
                XboxButtons.Add(Buttons.RightShoulder, "*");
                XboxButtons.Add(Buttons.RightTrigger, "+");
                XboxButtons.Add(Buttons.LeftShoulder, "-");
                XboxButtons.Add(Buttons.LeftTrigger, ",");
            }
        }

        public string Button(Buttons Button)
        {
            return XboxButtons[Button];
        }
        public string Button(string button)
        {
            if (button.Contains("[("))
            {
                string buttonEmDiante = button.Remove(0, button.IndexOf("[(") + 2);
                int FinalIndex = buttonEmDiante.IndexOf(")]");
                string finalButtonName = buttonEmDiante.Remove(FinalIndex, buttonEmDiante.Length - FinalIndex);
                string ButtonName = finalButtonName.ToLower();

                if (ButtonName.Contains("leftstick"))
                    return " ";
                if (ButtonName.Contains("dpad"))
                    return "!";
                if (ButtonName.Contains("rightstick"))
                    return '"'.ToString();
                if (ButtonName.Contains("back"))
                    return "#";
                if (ButtonName.Contains("guide"))
                    return "$";
                if (ButtonName.Contains("start"))
                    return "%";
                if (ButtonName.Contains("rb"))
                    return "*";
                if (ButtonName.Contains("rt"))
                    return "+";
                if (ButtonName.Contains("lb"))
                    return "-";
                if (ButtonName.Contains("lt"))
                    return ",";
                if (ButtonName.Contains("x"))
                    return "&";
                if (ButtonName.Contains("a"))
                    return "'";
                if (ButtonName.Contains("y"))
                    return "(";                
                if (ButtonName.Contains("b"))
                    return ")";             
            }

            return String.Empty;
        }

        public SpriteFont LoadFont(string FontPlace, string Name)
        {
            SpriteFont CustomFont = Engine.Content.Load<SpriteFont>(FontPlace);

            Fonts.Add(Name, CustomFont);

            return CustomFont;
        }

        public SpriteFont GetFont(string FontName)
        {
            if (Fonts.ContainsKey(FontName))
                return Fonts[FontName];
            else
                return null;
        }
    }
}
